<template>
  <section>
    <van-sticky>
      <PageHeader title="个人介绍" />
    </van-sticky>

    <van-field
      v-model="member.intro"
      rows="5"
      autosize
      label="个人介绍"
      type="textarea"
      placeholder="请输入个人介绍"
      v-if="member"
    />

    <div class="pd20">
      <van-button round block type="primary" @click="saveMemberProfile">保 存</van-button>
    </div>
  </section>
</template>

<script>
  import {
    memberSaveBaseInfoUri
  } from "../../../common/api";
  import PageHeader from "../../componets/PageHeader";
  import Vue from 'vue';
  import {Sticky, Field, Button, Toast} from 'vant';
  Vue.use(Sticky).use(Field).use(Button).use(Toast);

  export default {
    components: {
      PageHeader
    },
    data() {
      return {
        member: null,
        user: null,
      }
    },
    methods: {
      saveMemberProfile() {
        const that = this;
        const formData = {
          intro: this.member.intro
        };
        that.$http.post(memberSaveBaseInfoUri, formData, {emulateJSON: true}).then(response => {
          let {msg, code, data} = response.body
          if (code != 0) {
            Toast.fail(msg)
          } else {
            that.member = data.member;
            that.GLOBAL.memberProfile = data.member;
            Toast.success('保存成功');
            that.$router.back();
          }
        }, response => {
          Toast.fail('发生错误')
        })
      }
    },
    mounted: function () {
      this.member = this.GLOBAL.memberProfile;
      this.user = this.GLOBAL.userInfo;

      this.EventBus.$on('OnUserLogined', () => {
        this.user = this.GLOBAL.userInfo;
        this.member = this.GLOBAL.memberProfile;
      });
    }
  }
</script>

<style scoped>

</style>
